//
//  BCBaseNavigationViewController.swift
//  Blockfolio
//
//  Created by Maybe on 09/05/2021.
//

import UIKit

class BCBaseNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let navBar = navigationBar
        navBar.isTranslucent = false
        navBar.barTintColor = UIColor.white
        navBar.barStyle = UIBarStyle.black
        navBar.shadowImage = UIImage()
//        navBar.titleTextAttributes = [
//            NSAttributedString.Key.font = UIFont.boldSystemFont(ofSize: 18),
//            NSAttributedString.Key.foregroundColor = Color_Title
//        ]
        var att:[NSAttributedString.Key : AnyObject] = [:]
        att[.font] = UIFont.boldSystemFont(ofSize: 18)
        att[.foregroundColor] = Color_Title
        navBar.titleTextAttributes = att
        
        panGestureBack()
        
        
        // Do any additional setup after loading the view.
    }
    
    func panGestureBack() {
        let target = interactivePopGestureRecognizer?.delegate
        let pan = UIPanGestureRecognizer(target: target, action: Selector(("handleNavigationTransition:")))
        pan.delegate = self
        view.addGestureRecognizer(pan)
        interactivePopGestureRecognizer?.isEnabled = false
    }
    
    func gestureRecognizerShouldBegin(_ gesture: UIGestureRecognizer) -> Bool {
        if children.count == 1 {
            return false
        }else {
            //可返回的手势作用区域
            if let pan = gesture as? UIPanGestureRecognizer {
                let locationX  = Screen_width * 0.15
                let point = pan.translation(in: self.view)
                let state = gesture.state
                if state == .began || state == .possible {
                    let location = gesture.location(in: self.view)
                    let x = location.x.truncatingRemainder(dividingBy: Screen_width)
                    if point.x > 0 && x < locationX {
                        return true
                    }
                }
            }
            return false
        }
    }
    
    //拦截push
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if children.count > 0 {
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.leftItem("back-light21x21", highlightedImage: "back-light21x21", target: self, action: #selector(didTappedBackButton(_:)))
            viewController.hidesBottomBarWhenPushed = true
        }else {
            viewController.hidesBottomBarWhenPushed = false
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc fileprivate func didTappedBackButton(_ button: UIBarButtonItem){
        popViewController(animated: true)
    }
    
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
